package cn.itcast.nems.product.web.controller.o;

import cn.hutool.core.lang.Assert;
import cn.itcast.ic.common.exception.v2.BizExceptionProducer;
import cn.itcast.nems.common.account.AccountHolder;
import cn.itcast.nems.common.account.AccountInfo;
import cn.itcast.nems.product.dao.dataobject.ProductDO;
import cn.itcast.nems.product.dto.ClassTypeCampusDTO;
import cn.itcast.nems.product.scheduler.TuitionFeeService;
import cn.itcast.nems.product.service.ProductService;
import cn.itcast.nems.product.web.vo.PriceChangeAssembler;
import cn.itcast.nems.product.web.vo.PriceChangeVO;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * <p>
 * 产品相关
 * </p>
 *
 * @author liyong
 * @since 2023/10/17 9:42 AM
 **/
@RestController
@RequestMapping("/o/product")
public class ProductController {
    private final static Logger logger = LoggerFactory.getLogger(ProductController.class);

    @Autowired
    private ProductService productService;
    @Autowired
    private TuitionFeeService tuitionFeeService;

    // 批量产品价格变更
    @PostMapping("/prices")
    public ResponseEntity<String> priceChanges(@RequestBody List<PriceChangeVO> priceChanges) {
        logger.info("priceChanges 请求参数: {}", priceChanges);
        try {
            Assert.isTrue(priceChanges != null && !priceChanges.isEmpty(), BizExceptionProducer.badRequest("班级类型不能为空"));
            final List<ClassTypeCampusDTO> dtos = PriceChangeAssembler.toClassTypeCampusDTO(priceChanges);
            final List<ProductDO> products = this.productService.findProducts(dtos);
            if (products != null && !products.isEmpty()) {
                try {
                    String operator = "FlinkCDC";
                    final AccountInfo accountInfo = new AccountInfo(operator, operator, "", "");
                    AccountHolder.setAccountInfo(accountInfo);
                    logger.warn("FlinkCDC触发价格变更，涉及的产品数据:{}", products.size());
                    final Set<String> classTypeIds = dtos.stream().map(d -> d.getClassTypeId()).collect(Collectors.toSet());
                    this.tuitionFeeService.flushProductPrice(products, operator, classTypeIds);
                } finally {
                    AccountHolder.clear();
                }
            }
            return ResponseEntity.ok().body("OK");
        } catch (Exception ex) {
            logger.warn(ex.getMessage(), ex);
        }
        return ResponseEntity.ok().body("ERROR");
    }

    // 产品价格变更
    @PostMapping("/price")
    public ResponseEntity<String> priceChange(@RequestBody PriceChangeVO priceChange) {
        logger.info("请求参数: {}", priceChange);
        final String classTypeId = priceChange.getClassTypeId();
        Assert.isTrue(StringUtils.hasText(classTypeId), BizExceptionProducer.badRequest("班级类型不能为空"));
        final List<ProductDO> products = this.productService.findProducts(classTypeId, priceChange.getCampusIds());
        if (products != null && !products.isEmpty()) {
            try {
                String operator = "FlinkCDC";
                final AccountInfo accountInfo = new AccountInfo(operator, operator, "", "");
                AccountHolder.setAccountInfo(accountInfo);
                this.tuitionFeeService.flushProductPrice(products, operator, Set.of(classTypeId));
            } finally {
                AccountHolder.clear();
            }
        }
        return ResponseEntity.ok().body("OK");
    }
}
